/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.demo.demos.web;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.demos.tools.HttpUtils;
import com.example.demo.demos.tools.SendPost;
import com.fasterxml.jackson.annotation.JsonFormat;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Controller
public class BasicController {
    private static String ip = "http://10.122.54.87:19000/kd/pushRecord";
    static final Logger logger = LoggerFactory.getLogger(BasicController.class);

    @RequestMapping(value = "/facePush", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public void hello(@RequestParam Map<String, Object> user,
                      @RequestPart("backgroundImage") MultipartFile backgroundImage) {
        logger.info("报警参数是：========" + user);
        String savePath = "/www/wwwroot/html/dist/img/";
        logger.info("文件保存路径:" + savePath);
        // 创建保存路径的目录
        File saveDirectory = new File(savePath);
        if (!saveDirectory.exists()) {
            saveDirectory.mkdirs();
        }
        // 处理接收到的表单字段和图片数据
        if ( !backgroundImage.isEmpty()) {
            System.out.println("收到图片数据========");
            try {
                // 获取图片的信息
//                String faceImageName = faceImage.getOriginalFilename();
//                byte[] faceImageData = faceImage.getBytes();
                Date date = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String nowTime = format.format(date);
                long time = date.getTime();
                String backgroundImageName = backgroundImage.getOriginalFilename();
                byte[] backgroundImageData = backgroundImage.getBytes();

                // 保存图片到指定路径
//                if (!StringUtils.isEmpty(faceImageName)) {
//                    String faceImagePath = savePath + File.separator + faceImageName + "faceImage.jpeg";
//                    saveFile(faceImageData, faceImagePath);
//                }
                if (!StringUtils.isEmpty(backgroundImageName)) {
                    String backgroundImagePath = savePath + File.separator + "shenze-xingzheng-" + time + ".jpeg";
                    saveFile(backgroundImageData, backgroundImagePath);
                    logger.info("文件保存成功--------");
//报文格式 {"imgUrl": "dsf3245ji34om546nijiojsdr2.jpg,fjd298fjwgkofejg390jg3wjg.jpg", "vId": "e328590468386036", "hId": "h32454396i03468", "eId": "e328590468386036run20220621", "eType": "run", "startTime": "2022-06-21 10:30:26", "timestamp": 1655778626}
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("hId", "shenze");
                    jsonObject.put("vId", "xingzheng");
                    jsonObject.put("eId", "xingzheng2024-03-26flow");
                    jsonObject.put("imgUrl", "shenze-xingzheng-" + time + ".jpg");
                    jsonObject.put("eType", "face");
                    jsonObject.put("startTime", nowTime);
                    jsonObject.put("timestamp", time);
                    //发送违规类型
                    SendPost.post(ip, jsonObject);
                }
            } catch (Exception e) {
                logger.error("文件保存失败");
                e.printStackTrace();
            }
        } else {
            logger.info("没有接收到图片");
        }
    }
    private void saveFile(byte[] fileData, String filePath) throws IOException {
        FileOutputStream fos = new FileOutputStream(filePath);
        fos.write(fileData);
        fos.close();
    }

//    public static void main(String[] args) throws Exception {
//
////报文格式 {"imgUrl": "dsf3245ji34om546nijiojsdr2.jpg,fjd298fjwgkofejg390jg3wjg.jpg", "vId": "e328590468386036", "hId": "h32454396i03468", "eId": "e328590468386036run20220621", "eType": "run", "startTime": "2022-06-21 10:30:26", "timestamp": 1655778626}
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("hId", "shenze");
//        jsonObject.put("vId", "xingzheng");
//        jsonObject.put("eId", "xingzheng2024-03-26flow");
//        jsonObject.put("imgUrl", "backgroundImagePath");
//        jsonObject.put("eType", "run");
//        jsonObject.put("startTime", "2022-06-21 10:30:26");
//        jsonObject.put("timestamp", 12121);
//        String s = jsonObject.toJSONString();
//        //发送违规类型
////        SendPost.post(ip, jsonObject);
//        HttpUtils.sendPostRequestWithJson(ip, s);
//    }

}
